﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports Microsoft.VisualBasic.ControlChars
Imports GenDotNet.TemplateSupport
Imports GenDotNet.Task.Common
Imports GenDotNet.TemplateSupport.Strings
Imports GenDotNet.ObjectModel

Public Module Extensions
   Private mCurrentTemplate As DotNetBase

   Friend Property CurrentTemplate() As DotNetBase
      Get
         Return mCurrentTemplate
      End Get
      Set(ByVal value As DotNetBase)
         mCurrentTemplate = value
      End Set
   End Property

   <System.Runtime.CompilerServices.Extension()> _
   Public Function WithSemiColons(ByVal elements As IEnumerable(Of XElement)) As String
      Return mCurrentTemplate.WithSemiColons(elements)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function WithSemiColons(ByVal element As XElement) As String
      Return mCurrentTemplate.WithSemiColons(element)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function WithSemiColons(ByVal code As String) As String
      Return mCurrentTemplate.WithSemiColons(code)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function WithSpace(ByVal s As String) As String
      Return s & " "
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function WithSpaceAround(ByVal s As String) As String
      Return " " & s & " "
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function DotsAtCaps(ByVal s As String) As String
      Dim chars = s.ToCharArray()
      Dim sb = New Text.StringBuilder(20)
      For i = 0 To chars.Length - 1
         If Char.IsUpper(chars(i)) And i > 0 Then
            sb.Append(".")
         End If
         sb.Append(chars(i))
      Next
      Return sb.ToString()
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function Output(ByVal val As Boolean) As String
      If val Then
         Return trueString
      Else
         Return falseString
      End If
   End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function GetPrimaryKeyParameters(ByVal objectData As IObjectData) As ParameterInfo()
   '   Return (From prop In objectData.PrimaryKeys _
   '      Select New ParameterInfo(prop.Name, prop.NetType)).ToArray
   'End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function GeneratedName(ByVal name As String) As String
   '   Return "gen" & name
   'End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function DesignerName(ByVal name As String) As String
   '   Return name & ".Designer"
   'End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function CollectionName(ByVal name As String) As String
   '   Return name & "Collection"
   'End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function ReadonlyItemName(ByVal name As String) As String
   '   Return name & "Info"
   'End Function

   '<System.Runtime.CompilerServices.Extension()> _
   'Public Function ReadonlyListName(ByVal name As String) As String
   '   Return name & "List"
   'End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWith(ByVal list As IEnumerable(Of String), ByVal separator As String) As String
      If list Is Nothing OrElse list.Count = 0 Then
         Return String.Empty
      End If
      Dim sb As New Text.StringBuilder
      For i = 0 To list.Count - 1
         sb.Append(list(i))
         If i = list.Count - 1 Then
            Return sb.ToString
         End If
         sb.Append(separator)
      Next
      Return String.Empty
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithComma(ByVal list As IEnumerable(Of String)) As String
      Return ConcatenateWith(list, commaString)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithCommaNewLine(ByVal list As IEnumerable(Of String)) As String
      Return ConcatenateWith(list, commaString & nl)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
    Public Function ConcatenateWith(ByVal list As IEnumerable(Of XElement), ByVal separator As String) As String
      If list Is Nothing OrElse list.Count = 0 Then
         Return String.Empty
      End If
      Dim sb As New Text.StringBuilder
      For i = 0 To list.Count - 1
         sb.Append(list(i).Value)
         If i < list.Count - 1 Then
            sb.Append(separator)
         End If
      Next
      Return sb.ToString
   End Function

   <System.Runtime.CompilerServices.Extension()> _
    Public Function ConcatenateWithComma(ByVal list As IEnumerable(Of XElement)) As String
      Return ConcatenateWith(list, commaString)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithComma(ByVal element As XElement) As String
      Return ConcatenateWith(element.Descendants, commaString)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
    Public Function ConcatenateWithCommaNewLine(ByVal list As IEnumerable(Of XElement)) As String
      Return ConcatenateWith(list, commaString & nl)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithCommaNewLine(ByVal element As XElement) As String
      Return ConcatenateWith(element.Descendants, commaString & nl)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
    Public Function ConcatenateWithNewLine(ByVal list As IEnumerable(Of XElement)) As String
      Return ConcatenateWith(list, nl)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithNewLine(ByVal element As XElement) As String
      Return ConcatenateWith(element.Descendants, nl)
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function ConcatenateWithNewLine(ByVal text As String) As String
      Return text & nl
   End Function


   <System.Runtime.CompilerServices.Extension()> _
   Public Function Comment(ByVal commentString As String) As String
      Return String.Empty
   End Function

   <System.Runtime.CompilerServices.Extension()> _
   Public Function Comment(ByVal commentString As XElement) As String
      Return String.Empty
   End Function

End Module

Namespace VB
   Public Module Extensions
      '<System.Runtime.CompilerServices.Extension()> _
      '   Public Function CommaEndingNoWrap(ByVal last As Object, ByVal par As Object) As String
      '   Return CommaEnding(last, par, False)
      'End Function

      '<System.Runtime.CompilerServices.Extension()> _
      '   Public Function CommaEnding(ByVal last As Object, ByVal par As Object) As String
      '   Return CommaEnding(last, par, True)
      'End Function

      '<System.Runtime.CompilerServices.Extension()> _
      '   Public Function CommaEnding( _
      '         ByVal last As Object, _
      '         ByVal par As Object, _
      '         ByVal wrap As Boolean) _
      '         As String
      '   Return LiteralEnding(commaString, last, par, wrap)
      'End Function

      '<System.Runtime.CompilerServices.Extension()> _
      'Public Function LiteralEndingNoWrap( _
      '             ByVal literal As String, _
      '             ByVal last As Object, _
      '             ByVal par As Object) _
      '             As String
      '   Return LiteralEnding(literal, last, par, False)
      'End Function

      '<System.Runtime.CompilerServices.Extension()> _
      'Public Function LiteralEnding( _
      '            ByVal literal As String, _
      '            ByVal last As Object, _
      '            ByVal par As Object) _
      '            As String
      '   Return LiteralEnding(literal, last, par, True)
      'End Function

      '<System.Runtime.CompilerServices.Extension()> _
      'Public Function LiteralEnding( _
      '            ByVal literal As String, _
      '            ByVal last As Object, _
      '            ByVal par As Object, _
      '            ByVal wrap As Boolean) _
      '            As String
      '   If String.IsNullOrEmpty(literal) Then
      '      Return String.Empty
      '   End If
      '   If last IsNot Nothing AndAlso last IsNot par Then
      '      Return literal & If(wrap, wrapString, String.Empty)
      '   Else
      '      Return String.Empty
      '   End If
      'End Function

   End Module
End Namespace


